//
//  RentalDetailsViewController.m
//  YaDiuDiu
//
//  Created by 陈刚 on 2017/10/7.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "RentalDetailsViewController.h"
#import "RentalDetailsTableViewCell.h"

@interface RentalDetailsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *rentalTable;

@property (nonatomic, strong) NSMutableArray *rentalArray;

@property (nonatomic, strong) NSMutableArray *titleArray;

@property (nonatomic, strong) NSDictionary *payTypeDic;

@end

@implementation RentalDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
    
    [self loadData];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.rentalTable setContentOffset:CGPointMake(0, 0)];
}

- (void)setupView{
    [self showNav:@"订单详情"];
    [self.rentalTable registerNib:[UINib nibWithNibName:@"RentalDetailsTableViewCell" bundle:[NSBundle mainBundle]] forHeaderFooterViewReuseIdentifier:@"Cell"];
    self.payTypeDic = [[NSDictionary alloc]initWithObjectsAndKeys:@"支付宝",@"3",@"微信",@"2",@"银联",@"5",@"余额",@"4", nil];
}

- (void)loadData{
    
    NSString *ordertype = [self.detailsDic objectForKey:@"typeid"];
    
    if ([ordertype integerValue] == 2 ||[ordertype integerValue] == 3) {
        self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订  单  号",@"下单时间",@"支付金额",@"支付方式",@"用途",@"房源名称", nil];
    }
    if ([ordertype integerValue] == 9) {
        self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订  单  号",@"下单时间",@"支付金额",@"支付方式",@"用途", nil];
    }
    
    if ([ordertype integerValue] == 5) {
        self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订  单  号",@"下单时间",@"支付金额",@"支付方式",@"信贷名称", nil];
    }
    
    
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSString *orderid = [self.detailsDic objectForKey:@"id"];
    
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setValue:token forKey:@"verifyToken"];
    [parmas setValue:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setValue:orderid forKey:@"orderid"];
    [parmas setValue:ordertype forKey:@"orderTypeId"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/UserOrder/userOrderDetails",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"用户查看订单列表res:%@",responseObject);
        NSInteger state = [[responseObject objectForKey:@"status"] integerValue];
        NSString *msg = [responseObject objectForKey:@"msg"];
        NSDictionary *dataDic = [responseObject objectForKey:@"content"];
        if (state  == 200 && [dataDic isKindOfClass:[NSDictionary class]]) {
            [SVProgressHUD dismiss];
            NSString *orderdenumber = [dataDic objectForKey:@"ordernumber"];
            NSString *addtime = [self getyYearsTimeFormat:[dataDic objectForKey:@"addtime"]];
            NSString *ordermoney = [NSString stringWithFormat:@"%@元",[dataDic objectForKey:@"ordermoney"]];
            NSString *yongtu = @"";
            NSInteger ordertypeInt = [ordertype integerValue];
            switch (ordertypeInt) {
                case 2:
                    yongtu = [dataDic objectForKey:@"purpose"];
                    break;
                case 3:
                    yongtu = [dataDic objectForKey:@"yongtu"];
                    break;
                default:
                    break;
            }
            NSString *ordername = [dataDic objectForKey:@"ordername"];
            NSString *payType = [self.payTypeDic objectForKey:[dataDic objectForKey:@"payment"]];
            if ([ordertype integerValue] == 2 ||[ordertype integerValue] == 3) {
                self.rentalArray = [[NSMutableArray alloc]initWithObjects:orderdenumber,addtime,ordermoney,payType,yongtu,ordername, nil];
            }else{
                self.rentalArray = [[NSMutableArray alloc]initWithObjects:orderdenumber,addtime,ordermoney,payType,ordername, nil];
            }
            
            [self.rentalTable reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:msg];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.rentalArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *Identitfer  = @"Cell";
    RentalDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identitfer];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"RentalDetailsTableViewCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == self.rentalArray.count - 1) {
        [cell.valueLabel setTextColor:HexColorInt32_t(0F81FF)];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }else{
        [cell setAccessoryType:UITableViewCellAccessoryNone];
        [cell.valueLabel setTextColor:HexColorInt32_t(000000)];
    }
    
    [cell.itemLabel setText:[self.titleArray objectAtIndex:indexPath.row]];
    [cell.valueLabel setText:[self.rentalArray objectAtIndex:indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.rentalArray.count - 1) {
        NSLog(@"indexPath.row === %zd",indexPath.row);
    }
}

- (NSMutableArray *)rentalArray{
    if (!_rentalArray) {
        _rentalArray = [[NSMutableArray alloc]init];
    }
    return _rentalArray;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
